/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;

/**
 *
 * @author JDK
 */
public class DomainRestrictionRefinement extends Refinement {

    private String name = new String();
    private String property;
    private ExpandingHistory eh;
    private String corename = new String();
    
    private int viotype;
    
    private String prefix;
    private String standard;    
    private Vector<String> datarange;
            
//    public DomainRestrictionRefinement(RDFNode standard, String prefix, Property prop) {
//        this.name = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
//    }

    public DomainRestrictionRefinement(String name, String corename, ExpandingHistory eh) {
        this.name = name;
        this.eh = eh;
        this.corename = corename;
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }

    public DomainRestrictionRefinement(String standard, String prefix, String prop, int type) {
        this.name = prop + ".(" + prefix + standard + ")";
        this.corename = prop + ".(" + prefix + standard + ")";        
        this.standard = standard;
        this.prefix = prefix;
        this.property = prop;
        this.viotype = type;
        this.cc = new ConstraintConstructor(prefix, standard, prop);
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }

//    public DomainRestrictionRefinement(RDFNode standard, String prefix, ExpandingHistory eh, Property prop) {
//        // 안쓰는듯;;;;
//        this.eh = eh;
//        Vector<String> quantifiers = this.eh.getQuantifierSet();
//        Vector<PropertyArc> propertySet = this.eh.getPropertySet();
//
//        //여기에 가장 안쪽 .( >= 22)
//        this.name = prop.getLocalName().toString() + ".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
//        this.corename = prop.getLocalName().toString() + ".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
//        this.cc = new ConstraintConstructor(prefix, standard.asLiteral().getLexicalForm(), prop);
//
//        for (int i = 0; i < propertySet.size(); i++) {
//            PropertyArc pa = propertySet.get(i);
//            Property p = pa.getProperty();
//            String q = quantifiers.get(i);
//            if (pa.getDirection()) {
//                this.name = q + " " + p.getLocalName() + ".(" + this.name + ")";
//            } else {
//                this.name = q + " " + p.getLocalName() + "￣.(" + this.name + ")";
//            }
//
//        }
//        super.refinementType = RefinementType.DOMAINRESTRICTION;
//
//    }

    public DomainRestrictionRefinement(Vector standardSet, String prop) {
        String cons = new String();
        for (int i = 0; i < standardSet.size(); i++) {
            String r = standardSet.get(i).toString();
            if (i != standardSet.size() - 1) {
                cons += r + DLConstructors.CONJUNCTION;
            } else {
                cons += r;
            }
        }
        this.property = prop;        
        this.viotype = ValueInfoOfDP.DATARANGE;
        this.datarange = standardSet;        
        this.name = prop + ".(!=(" + cons + "))";
        this.corename = prop + ".(!=(" + cons + "))";
        this.cc = new ConstraintConstructor(cons);
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }

    public DomainRestrictionRefinement(String standard, String prefix, ExpandingHistory eh, String prop, int vtype) {
        this.eh = eh;
        this.viotype = vtype;
        this.property = prop;        
        this.standard = standard;
        this.prefix = prefix;
        
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();

        //여기에 가장 안쪽 .( >= 22)
        this.name = prop + ".(" + prefix + standard + ")";
        this.corename = prop + ".(" + prefix + standard + ")";
        this.cc = new ConstraintConstructor(prefix, standard, prop);

        for (int i = 0; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            String p = pa.getProperty();
            String q = quantifiers.get(i);
            if (pa.getDirection()) {
                this.name = q + " " + p + ".(" + this.name + ")";
            } else {
                this.name = q + " " + p + "￣.(" + this.name + ")";
            }
        }        
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }
    
    public DomainRestrictionRefinement(Vector standardSet, ExpandingHistory eh, String prop) {
        this.eh = eh;
        this.viotype = ValueInfoOfDP.DATARANGE;
        this.property = prop;
        this.datarange = standardSet;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();

        //여기에 가장 안쪽 .( >= 22)
        String cons = new String();
        for (int i = 0; i < standardSet.size(); i++) {
            String r = standardSet.get(i).toString();
            if (i != standardSet.size() - 1) {
                cons += r + DLConstructors.CONJUNCTION;
            } else {
                cons += r;
            }
        }
        
        this.name = prop + ".(!=(" + cons + "))";
        this.corename = prop + ".(!=(" + cons + "))";
        this.cc = new ConstraintConstructor("", cons, prop);

        for (int i = 0; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            String p = pa.getProperty();
            String q = quantifiers.get(i);
            if (pa.getDirection()) {
                this.name = q + " " + p + ".(" + this.name + ")";
            } else {
                this.name = q + " " + p + "￣.(" + this.name + ")";
            }
        }        
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }

    public ExpandingHistory getExpandingHistory() {
        return this.eh;
    }

    @Override
    public void generateRefinementName() {
        super.refinement_name = this.name;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public void setViotype(int viotype) {
        this.viotype = viotype;
    }

//    public void setPorne(String porne) {
//        this.porne = porne;
//    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }

    public void setDatarange(Vector<String> datarange) {
        this.datarange = datarange;
    }

    public int getViotype() {
        return viotype;
    }

//    public String getPorne() {
//        return porne;
//    }

    public String getPrefix() {
        return prefix;
    }

    public String getStandard() {
        return standard;
    }

    public Vector<String> getDatarange() {
        return datarange;
    }
    
    
    
        
    @Override
    public Refinement generateUpdatedRefinement(Vector<TargetInstance> instanceSet) {
        Refinement ref = new DomainRestrictionRefinement(this.name, this.corename, this.eh);
        ((DomainRestrictionRefinement)ref).setProperty(this.property);
        ((DomainRestrictionRefinement)ref).setViotype(this.viotype);
        if(this.viotype == ValueInfoOfDP.DATARANGE){
            ((DomainRestrictionRefinement)ref).setDatarange(this.datarange);            
        }else{
//            ((DomainRestrictionRefinement)ref).setPorne(this.porne);
            ((DomainRestrictionRefinement)ref).setPrefix(this.prefix);
            ((DomainRestrictionRefinement)ref).setStandard(this.standard);            
        }        
        
        ref.setBranchingType(this.branching_type);
        ref.setConstraintConstructor(this.cc);
        
        Vector<TargetInstance> oldinsSet = this.getInstanceSet();
        for (int i = 0; i < instanceSet.size(); i++) {
            TargetInstance r = instanceSet.get(i);
            if (oldinsSet.contains(r)) {
                ref.setInstanceSet(r);
            }
        }
        return ref;
    }

    @Override
    public String getCoreName() {
        return this.corename;
    }

    @Override
    public Vector<Resource> getConstraintMatcherInstance(Model model) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getName() {
        return this.name;
    }
}
